package com.my.easymall.web;


import com.my.easymall.domain.User;
import com.my.easymall.exception.EmailExistException;
import com.my.easymall.exception.UsernameExistException;
import com.my.easymall.service.UserService;
import com.my.easymall.utils.MD5Utils;
import com.my.easymall.utils.StringUtils;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

/**
 * @auther Summerday
 *
 * 注册验证
 */
@WebServlet("/RegistServlet")
public class RegistServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取所有请求参数
        Map<String, String[]> map = request.getParameterMap();
        //创建用户实体user
        User user = new User();

        //封装javabean
        try {
            BeanUtils.populate(user, map);
        } catch (IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
        //首先判断验证码
        String valistr = request.getParameter("valistr");

        //验证码非空校验
        if (StringUtils.isEmpty(valistr)) {
            request.setAttribute("validate_msg", "验证码不能为空");
            request.getRequestDispatcher("/regist.jsp").forward(request, response);
            return;
        }
        //获取session中的验证码
        HttpSession session = request.getSession();
        String code = (String) session.getAttribute("code");
        session.removeAttribute("code");

        //验证码相等校验
        if(!StringUtils.equalsIgnoreCase(code,valistr)){
            request.setAttribute("validate_msg", "验证码错误");
            request.getRequestDispatcher("/regist.jsp").forward(request, response);
            return;
        }
        //获取用户的验证密码
        String password2 = request.getParameter("password2");
        //用户名非空校验
        if (StringUtils.isEmpty(user.getUsername())) {
            //注册页面提示，用户名称不能为空
            request.setAttribute("user_msg", "用户名称不能为空");
            //请求转发到注册页面，可以共享数据
            request.getRequestDispatcher("/regist.jsp").forward(request, response);
            return;
        }
        //密码非空校验
        if (StringUtils.isEmpty(user.getPassword())) {
            request.setAttribute("pas_msg", "密码不能为空");
            request.getRequestDispatcher("/regist.jsp").forward(request, response);
            return;
        }
        //验证密码非空校验
        if (StringUtils.isEmpty(password2)) {
            request.setAttribute("pas2_msg", "确认密码不能为空");
            request.getRequestDispatcher("/regist.jsp").forward(request, response);
            return;
        }
        //昵称非空校验
        if (StringUtils.isEmpty(user.getNickname())) {
            request.setAttribute("nick_msg", "昵称不能为空");
            request.getRequestDispatcher("/regist.jsp").forward(request, response);
            return;
        }
        //邮箱非空校验
        if (StringUtils.isEmpty(user.getEmail())) {
            request.setAttribute("email_msg", "邮箱不能为空");
            request.getRequestDispatcher("/regist.jsp").forward(request, response);
            return;
        }


        //邮箱格式校验
        String reg = "^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$";
        if (!user.getEmail().matches(reg)) {
            request.setAttribute("email_msg", "邮箱格式错误");
            request.getRequestDispatcher("/regist.jsp").forward(request, response);
            return;
        }
        //密码一致性校验
        if (!StringUtils.equals(user.getPassword(), password2)) {
            request.setAttribute("pas2_msg", "两次密码不一致");
            request.getRequestDispatcher("/regist.jsp").forward(request, response);
            return;
        }

        //MD5加密
        user.setPassword(MD5Utils.md5(password2));
        //创建UserService
        UserService service = new UserService();
        try {
            service.registUser(user);
            //跳转回首页
            //定时刷新
            response.getWriter().write("<h1 align='center'>恭喜注册成功，3秒后跳转回首页<h1>");
            response.setHeader("refresh", "3;url=http://www.easymall.com/");
        }catch (UsernameExistException e){
            request.setAttribute("user_msg",e.getMessage());
            request.getRequestDispatcher("/regist.jsp").forward(request, response);
        }catch (EmailExistException e){
            request.setAttribute("email_msg",e.getMessage());
            request.getRequestDispatcher("/regist.jsp").forward(request, response);
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
